home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: locking
- Message-ID: <1996Jan12.104534.1741@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 12 Jan 96 10:45:33 WET
- References: <4d1kl2$64c@daphne.ecmwf.int>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <4d1kl2$64c@daphne.ecmwf.int> mab@ecmwf.co.uk (Baudouin Raoult)
- writes:
- > [stuff deleted]
- >
- > : For the class
- >
- > : struct Foo { void bar(); }
- >
- > : a method-invocation like:
- >
- > : Foo f;
- > : SharedObject<Foo> s(&f);
- > : s->bar(); // (1)
- >
- > : results in
- > :
- > : ((s.operator->()).operator->())->bar(); // = (1)
- >
- > : IMHO, the 'LockObject' temporary created in this expression should be
- > : destroyed after this line. To force this behavior from an unwilling
- > : compiler (;-) one can put (1) in brackets. So the brackets are used to
- > : restrict the lifetime of the temporary and not the one of the
- > : SharedObject instance.
- >
- > : Enno
- >
- > Thanks for showing some nterest to my original posting. I was
- > trying to establish a way to wrap/lock any object and them forget about
- > it. So the { } are not a satisfactory. It looks like my compiler
- > (cfront 3.??) is not right.
- >
- > Baudouin
- >
-
- Careful here! The question boils down to the lifetime of a temporary, in
- this case the LockObject. When is it destructed? There are differences
- between compilers currently in use: some compilers destruct the temporary
- at the end of the block in which it was created (in which case Enno's
- bracket trick will restrict the lifetime of the lock), but others may
- destruct the temporary right after the call to its operator->(), in which
- case the lock is released *before* the `bar()' member function is invoked.
- I think the draft standard says the temporary will be destructed at the
- `end of the full expression' (that is, an expression that is not part of
- some other expression), so Baudoin Raoult's trick should work, but not all
- compilers are up to date yet.
-
- All in all, it is currently impossible to rely on the lifetime of
- temporary if you want to write portable code. I would go for an explictly
- declared instance of the locking object instead - which unfortunately
- requires extra coding and/or interface replication.
-
- - Wil
-